﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XGameFrame.Controls
{
    public class Background : Control
    {
        #region Field Region

        private Texture2D movingImage;
        private int movingOffset;
        private bool isMoving;

        #endregion

        #region Property Region

        /// <summary>
        /// Gets the texture of the moving image for this background or set it
        /// </summary>
        public Texture2D MovingImage
        {
            get { return movingImage; }
            set { movingImage = value; }
        }


        /// <summary>
        /// Get the offset of the moving image or set it
        /// </summary>
        public int MovingOffset
        {
            get { return movingOffset; }
            set 
            {
                movingOffset = value;
                if(movingOffset < 0)
                {
                    movingOffset += MovingImage.Width;
                }
                if(movingOffset > MovingImage.Width)
                {
                    movingOffset -= MovingImage.Width;
                }
            }
        }

        /// <summary>
        /// Get moving status or set it
        /// </summary>
        public bool IsMoving
        {
            get { return isMoving; }
            set { isMoving = value; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new Background without any animation
        /// </summary>
        /// <param name="pStaticImage">Texture for Background</param>
        /// <param name="pScreenImage">Viewportsize</param>
        public Background(Texture2D pStaticImage, Rectangle pScreenImage)
            : base(pStaticImage,new Vector2(0,0))
        {
            Texture = pStaticImage;
            MovingImage = null;
            IsMoving = false;
            DestRectangle = pScreenImage;
            SourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
            Color = Color.White;
        }

        /// <summary>
        /// Creates a new Background with animation
        /// </summary>
        /// <param name="pStaticImage">Texture for Background</param>
        /// <param name="pMovingImage">Texture for moving Background</param>
        /// <param name="pScreenImage">Viewportsize</param
        public Background(Texture2D pStaticImage, Texture2D pMovingImage, Rectangle pScreenImage)
            : base(new Vector2(0, 0))
        {
            Texture = pStaticImage;
            MovingImage = pMovingImage;
            IsMoving = true;
            DestRectangle = pScreenImage;
            //SourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
            Color = Color.White;
        }

        #endregion

        #region Abstract Method Region

        public override void Update(GameTime gameTime)
        {

        }

        public override void Draw(SpriteBatch pSpriteBatch)
        {

            pSpriteBatch.Draw(Texture, DestRectangle, Color);            
            if (IsMoving)
            {
                pSpriteBatch.Draw(MovingImage, new Rectangle(-1 * MovingOffset, 0, MovingImage.Width, DestRectangle.Height), Color);

                if(MovingOffset > MovingImage.Width - DestRectangle.Width) 
                {
                    pSpriteBatch.Draw(MovingImage, new Rectangle((-1 * MovingOffset) + MovingImage.Width, 0, MovingImage.Width, DestRectangle.Height), Color); 
                }
            }
        }

        public override void HandleInput(PlayerIndex playerIndex)
        {
            if(InputHandler.KeyDown(Keys.Left))
            {
                MovingOffset -= 2;
            }

            if(InputHandler.KeyDown(Keys.Right))
            {
                MovingOffset += 2;
            }
        }

        public override void OnHover(PlayerIndex playerIndex)
        {
        }

        #endregion

    }
}
